package com.hunk.utils.relfect;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

import com.hunk.core.auth.entity.User;
import com.hunk.utils.io.Print;

public class ClazzInfo {
	
	public static Object[] getNonStaticPropertiesValue(User entity) throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
		Class<?> clazz = entity.getClass();
		List<Object> objects = new ArrayList<Object>();
		for (Field field : clazz.getDeclaredFields()) {
			if (!Modifier.isStatic(field.getModifiers())) {
				PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz);
				Method rM = pd.getReadMethod();//获得读方法
				objects.add(rM.invoke(entity));
			}
		}
		return objects.toArray();
	}
	
	public static String[] getNonStaticPropertiesName(User entity) {
		Class<?> clazz = entity.getClass();
		List<String> objects = new ArrayList<String>();
		for (Field field : clazz.getDeclaredFields()) {
			if (!Modifier.isStatic(field.getModifiers())) {
				objects.add(field.getName());
			}
		}
		return objects.toArray(new String[]{});
	}
	
	static class Test {
		public static void main(String[] args) {
			User user = new User("hunk","hunk");
			try {
				Print.println(ClazzInfo.getNonStaticPropertiesValue(user));
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IntrospectionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
